package com.cloud.tsp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.job.EvStatistics;
import com.cloud.api.domain.report.*;
import com.cloud.api.utils.ExcelUtils;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.config.MysqlConfig;
import com.cloud.tsp.mapper.EvStatisticsMapper;
import com.cloud.tsp.service.EmailService;
import com.cloud.tsp.service.IEvStatisticsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.Locale;

/**
 * @author 杨斌
 * 2025-01-2025-01-14
 */
@Service("evStatisticsService")
public class EvStatisticsServiceImpl extends ServiceImpl<EvStatisticsMapper, EvStatistics> implements IEvStatisticsService {
    @Resource
    private EvStatisticsMapper evStatisticsMapper;

    @Resource
    private MessageSource messageSource;

    @Autowired
    private MysqlConfig mysqlConfig;

    @Autowired
    private EmailService emailService;
    @Override
    public void exportProvinceDistributionList(QueryDistribution para, HttpServletResponse response) {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.province.distribution";
        List<ProvinceDistribution> list = evStatisticsMapper.selectProvinceDistribution(para);

        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        ExcelUtils.export(response,fileName,list, ProvinceDistribution.class);
    }

    @Override
    public void exportVehiclesOnLineList(QueryDistribution para, HttpServletResponse response) {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicles.onLine";
        List<VehiclesOnLine> list = evStatisticsMapper.exportVehiclesOnLineList(para);

        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        ExcelUtils.export(response,fileName,list, VehiclesOnLine.class);
    }

    @Override
    public void exportVehiclesModelList(QueryDistribution para, HttpServletResponse response) {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicles.model";
        List<VehicleModelCondition> list = evStatisticsMapper.exportVehiclesModelList(para, mysqlConfig.getDataSource());

        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        ExcelUtils.export(response,fileName,list, VehicleModelCondition.class);
    }

    @Override
    public void exportVehiclesEnergyList(QueryDistribution para, HttpServletResponse response) {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicles.energy";
        List<VehiclesEnergy> list = evStatisticsMapper.exportVehiclesEnergyList(para, mysqlConfig.getDataSource());

        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        ExcelUtils.export(response,fileName,list, VehiclesEnergy.class);
    }

    @Override
    public TableDataInfo<ProvinceDistribution> selectProvinceDistributionList(QueryProvinceDistribution para) {
        Page<ProvinceDistribution> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<ProvinceDistribution> data = evStatisticsMapper.selectProvinceDistributionList(page,para);
        return TableDataInfo.build(data);
    }

    @Override
    public TableDataInfo<VehiclesOnLine> selectVehiclesOnLineList(QueryProvinceDistribution para) {
        Page<VehiclesOnLine> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<VehiclesOnLine> data = evStatisticsMapper.selectVehiclesOnLineList(page,para);
        return TableDataInfo.build(data);
    }

    @Override
    public TableDataInfo<VehicleModelCondition> selectVehiclesModelList(QueryProvinceDistribution para) {
        Page<VehicleModelCondition> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<VehicleModelCondition> data = evStatisticsMapper.selectVehiclesModelList(page,para, mysqlConfig.getDataSource());
        return TableDataInfo.build(data);
    }

    @Override
    public TableDataInfo<VehiclesEnergy> selectVehiclesEnergyList(QueryProvinceDistribution para) {
        Page<VehiclesEnergy> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<VehiclesEnergy> data = evStatisticsMapper.selectVehiclesEnergyList(page,para, mysqlConfig.getDataSource());
        return TableDataInfo.build(data);
    }

    @Override
    public void exportMailProvinceDistributionList() {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.province.distribution";
        LocalDate firstDayOfLastMonth = YearMonth.now().minusMonths(1).atDay(1);
        LocalDate firstDayOfCurrentMonth = YearMonth.now().atDay(1);
        List<ProvinceDistribution> list = evStatisticsMapper.exportMailProvinceDistributionList(firstDayOfLastMonth, firstDayOfCurrentMonth);

        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 导出Excel文件到ByteArrayOutputStream
            ExcelUtils.export(outputStream, fileName, list, ProvinceDistribution.class);
            // 获取字节数组副本，用于后续邮件发送
            byte[] fileBytes = outputStream.toByteArray();
            // 异步发送邮件
            emailService.sendEmailAsync("车辆各省市运行情况导出", "请查看附件中的车辆各省市运行情况。", fileName + ".xlsx", fileBytes);
        } catch (IOException e) {
            log.error("导出Excel文件失败", e);
        }
    }

    @Override
    public void exportMailVehiclesOnLineList() {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicles.onLine";
        LocalDate firstDayOfLastMonth = YearMonth.now().minusMonths(1).atDay(1);
        LocalDate firstDayOfCurrentMonth = YearMonth.now().atDay(1);
        List<VehiclesOnLine> list = evStatisticsMapper.exportMailVehiclesOnLineList(firstDayOfLastMonth, firstDayOfCurrentMonth);

        fileName = messageSource.getMessage(fileName, null, fileName, locale);

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 导出Excel文件到ByteArrayOutputStream
            ExcelUtils.export(outputStream, fileName, list, VehiclesOnLine.class);
            // 获取字节数组副本，用于后续邮件发送
            byte[] fileBytes = outputStream.toByteArray();
            // 异步发送邮件
            emailService.sendEmailAsync("车辆上线情况导出", "请查看附件中的车辆上线情况。", fileName + ".xlsx", fileBytes);
        } catch (IOException e) {
            log.error("导出Excel文件失败", e);
        }
    }

    @Override
    public void exportMailVehiclesModelList() {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicles.model";

        LocalDate firstDayOfLastMonth = YearMonth.now().minusMonths(1).atDay(1);
        LocalDate firstDayOfCurrentMonth = YearMonth.now().atDay(1);

        List<VehicleModelCondition> list = evStatisticsMapper.exportMailVehiclesModelList(firstDayOfLastMonth, firstDayOfCurrentMonth, mysqlConfig.getDataSource());

        fileName = messageSource.getMessage(fileName, null, fileName, locale);

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 导出Excel文件到ByteArrayOutputStream
            ExcelUtils.export(outputStream, fileName, list, VehicleModelCondition.class);
            // 获取字节数组副本，用于后续邮件发送
            byte[] fileBytes = outputStream.toByteArray();
            // 异步发送邮件
            emailService.sendEmailAsync("车型运行情况导出", "请查看附件中的车型运行情况。", fileName + ".xlsx", fileBytes);
        } catch (IOException e) {
            log.error("导出Excel文件失败", e);
        }
    }

    @Override
    public void exportMailVehiclesEnergyList() {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicles.energy";
        LocalDate firstDayOfLastMonth = YearMonth.now().minusMonths(1).atDay(1);
        LocalDate firstDayOfCurrentMonth = YearMonth.now().atDay(1);
        List<VehiclesEnergy> list = evStatisticsMapper.exportMailVehiclesEnergyList(firstDayOfLastMonth, firstDayOfCurrentMonth, mysqlConfig.getDataSource());

        fileName = messageSource.getMessage(fileName, null, fileName, locale);

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 导出Excel文件到ByteArrayOutputStream
            ExcelUtils.export(outputStream, fileName, list, VehiclesEnergy.class);
            // 获取字节数组副本，用于后续邮件发送
            byte[] fileBytes = outputStream.toByteArray();
            // 异步发送邮件
            emailService.sendEmailAsync("车辆能耗数据分析导出", "请查看附件中的车辆能耗数据分析。", fileName + ".xlsx", fileBytes);
        } catch (IOException e) {
            log.error("导出Excel文件失败", e);
        }
    }
}
